package com.aim.analysis.records;

import com.aim.util.HashMapOfList;
import com.aim.util.ListUtil;
import com.aim.util.MeanVarPair;
import com.aim.util.Pair;
import com.aim.util.extractors.IntegerValueExtractor;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * User: Avraham Shvartzon
 * Date: 6/24/14
 */
public class HitList {

    public List<Hit> hits;

    public HashMapOfList<Pair<String, Integer>, Game> descriptionOrderMap = new HashMapOfList<Pair<String, Integer>, Game>();
    public HashMapOfList<String, Game> descriptionMap = new HashMapOfList<String, Game>();
    public HashMapOfList<Integer, Game> orderMap = new HashMapOfList<Integer, Game>();

    public HitList(List<Hit> hits) {
        this.hits = hits;
    }

    public void init(){
        loadMaps();
        printReport();

    }

    private void loadMaps() {
        List<Game> games = HitUtil.flattGames(hits);
        for (Game game : games) {
            GameKey key = game.getKey();
            Pair<String, Integer> descAndOrderKey = new Pair<String, Integer>(key.gameDescription, key.gameNumber);
            if (key.gameNumber == 1){
                continue;
            }
            descriptionOrderMap.put(descAndOrderKey, game);
            descriptionMap.put(key.gameDescription, game);
            orderMap.put(key.gameNumber, game);
        }
    }

    private void printReport() {
        printMapReport(descriptionOrderMap);
        printMapReport(descriptionMap);
        printMapReport(orderMap);
    }

    private void printMapReport(HashMapOfList<? , Game> map){
        Set<? extends Map.Entry<?, List<Game>>> entries = map.entrySet();
        for (Map.Entry<?, List<Game>> entry : entries) {
            MeanVarPair p = ListUtil.meanAndVariance(entry.getValue(), scoreExtractor);
            System.out.println(entry.getKey() + "\t" + p.mean() + "\t" + p.variance());
        }


    }

    private static IntegerValueExtractor<Game> scoreExtractor = new IntegerValueExtractor<Game>() {
        @Override
        public Integer getValue(Game game) {
            return game.getFinalScore();
        }
    };

    public static void main(String[] args) {
        List<Hit> hits = Hit.getHits("20140616", "20140619");
        HitList hitList = new HitList(hits);
        hitList.init();
        assert hits.size() > 0;
    }


}
